var app = getApp()
var pageObject = {
  data: {
    unfinishedOrderList: '',
    historyOrderList: '',
    indexOrderList: [],
    leftPartClass: 'header-left-part active',
    rightPartClass: 'header-right-part',
    tabState: 'left'
  },
  onLoad: function() { 
    var loginUserId = wx.getStorageSync('LOGINUSER_ID') || '';
    if(loginUserId == ''){
      wx.redirectTo({
        url: '../index/index?redirect=order-center/order-center'
      })
    }else{ 
      this.showUnfinishedOrderList();
    }
  }, 
  onShow: function () {
    this.showUnfinishedOrderList(); 
  },
  showDoingOrderList: function(){ 
    var loginUserId = wx.getStorageSync('LOGINUSER_ID') || '';
    var _this = this;
    wx.request({ 
      url: app.globalData.buy_doing_url,
      data: {userId:loginUserId,finish:'0'},
      method: 'POST', 
      success: function (res) {
        var resultData = res.data;
        if(resultData.errno == 0){
          _this.setData({indexOrderList: resultData.data}) 
        }
      },
      fail: function () {
        setTimeout(function () {
          wx.showToast({
            title: "加载失败"
          })
        }, 100)
      }
    }) 
  },
  showDoneOrderList: function(){ 
    var loginUserId = wx.getStorageSync('LOGINUSER_ID') || '';
    var _this = this;
    wx.request({ 
      url: app.globalData.buy_doing_url,
      data: {userId:loginUserId,finish:'1'},
      method: 'POST', 
      success: function (res) {
        var resultData = res.data;
        if(resultData.errno == 0){
          _this.setData({indexOrderList: resultData.data}) 
        }
      },
      fail: function () {
        setTimeout(function () {
          wx.showToast({
            title: "加载失败"
          })
        }, 100)
      }
    }) 
  },
  showHistoryOrderList: function() {
    this.showDoneOrderList(); 
    this.setData({
      rightPartClass: 'header-right-part active',
      leftPartClass: 'header-right-part',
      tabState: 'right'
    })
  },
  showUnfinishedOrderList: function() {
    this.showDoingOrderList();
    this.setData({
      rightPartClass: 'header-right-part',
      leftPartClass: 'header-right-part active',
      tabState: 'left'
    })
  },
  cancel: function(event) {  
    var _this = this;
    wx.request({ 
      url: app.globalData.buy_cancel_url,
      data: {id:event.currentTarget.dataset.id,finish:-1},
      method: 'POST', 
      success: function (res) {
        var resultData = res.data;
        if(resultData.errno == 0){
           _this.showUnfinishedOrderList();
           app.showSuccess(resultData.message);
        }
      } 
    }) 
  },
  commit: function(event) {   
    wx.redirectTo({
      url: '../order-confirm/order-confirm?orderId=' + event.currentTarget.dataset.id +'&productId='+ event.currentTarget.dataset.productid + '&nums=' + event.currentTarget.dataset.nums
    })
  }
}

Page(pageObject)
